package com.algorithm.one2fifty;

/**
 * @author tuzhijin
 */
public class RemoveElement_27 {

    public static void main(String[] args) {
        int[] nums = {3, 2, 2, 3, 4};
        int val = 3;
        System.out.println(removeElement(nums, val));
    }

    public static int removeElement(int[] nums, int val) {
        int slow = 0;
        int n = nums.length;
        // 快指针遍历整个数组，慢指针满足条件才移动
        for (int fast = 0; fast < n; fast++) {
            // 不等于目标元素，慢指针才移动
            if (nums[fast] != val) {
                nums[slow++] = nums[fast];
            }
        }
        // 返回慢指针的长度
        return slow;
    }
}
